package cn.patient.pp.module.report.controller.admin;


import cn.patient.pp.framework.datapermission.core.annotation.DataPermission;
import cn.patient.pp.framework.idempotent.core.annotation.Idempotent;
import cn.patient.pp.framework.security.core.LoginUser;
import cn.patient.pp.framework.security.core.util.SecurityFrameworkUtils;
import cn.patient.pp.module.report.controller.admin.vo.PatientInfoPageReqVO;
import cn.patient.pp.module.report.controller.admin.vo.PatientInfoRespVO;
import cn.patient.pp.module.report.controller.admin.vo.PatientInfoSaveReqVO;
import cn.patient.pp.module.report.dal.dataobject.report.PatientInfoDO;
import cn.patient.pp.module.report.service.PatientInfoService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.patient.pp.framework.common.pojo.PageParam;
import cn.patient.pp.framework.common.pojo.PageResult;
import cn.patient.pp.framework.common.pojo.CommonResult;
import cn.patient.pp.framework.common.util.object.BeanUtils;
import static cn.patient.pp.framework.common.pojo.CommonResult.success;

import cn.patient.pp.framework.excel.core.util.ExcelUtils;

import cn.patient.pp.framework.apilog.core.annotation.ApiAccessLog;
import static cn.patient.pp.framework.apilog.core.enums.OperateTypeEnum.*;


/**
 * @author liqingsong
 * @version 1.0
 * Create by 2025/4/24
 * 县级审批
 */
@Tag(name = "管理后台 - 县级审批")
@RestController
@RequestMapping("/report/county")
@Validated
public class CountyController {

    @Resource
    private PatientInfoService patientInfoService;


    @PostMapping("/update")
    @Idempotent(timeout = 10, message = "重复请求，请稍后重试")
    @Operation(summary = "县区更新精神病患者症状信息")
    @PreAuthorize("@ss.hasPermission('report:approval:confirm')")
    @ApiAccessLog(operateType = UPDATE)
    public CommonResult<Boolean> updatePatientInfo(@Valid @RequestBody PatientInfoSaveReqVO updateReqVO) {
        patientInfoService.updatePatientInfoDiagnosis(updateReqVO);
        return success(0,"更新患者诊断状态成功");
    }

    @DeleteMapping ("/delete")
    @ApiAccessLog(operateType = DELETE)
    @Operation(summary = "删除上报精神病患者信息")
    @Idempotent(timeout = 10, message = "重复请求，请稍后重试")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('report:approval:delete')")
    public CommonResult<Boolean> deletePatientInfo(@RequestParam("id") Long id) {
        patientInfoService.deletePatientInfo(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得上报精神病患者信息")
    @ApiAccessLog(operateType = GET)
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('report:patient-info:query')")
    public CommonResult<PatientInfoRespVO> getPatientInfo(@RequestParam("id") Long id) {
        PatientInfoDO patientInfo = patientInfoService.getPatientInfo(id);
        return success(BeanUtils.toBean(patientInfo, PatientInfoRespVO.class));
    }

    @PostMapping("/page")
//    @DataPermission(enable = true)
    @Operation(summary = "获得县区上报精神病患者信息分页")
    @ApiAccessLog(operateType = GET)
    @PreAuthorize("@ss.hasPermission('report:approval:query')")
    public CommonResult<PageResult<PatientInfoRespVO>> getPatientInfoPage(@RequestBody @Valid PatientInfoPageReqVO pageReqVO) {
        PageResult<PatientInfoDO> pageResult = patientInfoService.getPatientInfoPageByCounty(pageReqVO);
        return success(BeanUtils.toBean(pageResult, PatientInfoRespVO.class));
    }

//    @GetMapping("/export-excel")
//    @Operation(summary = "导出上报精神病患者信息 Excel")
//    @PreAuthorize("@ss.hasPermission('report:patient-info:export')")
//    @ApiAccessLog(operateType = EXPORT)
//    public void exportPatientInfoExcel(@Valid PatientInfoPageReqVO pageReqVO,
//                                       HttpServletResponse response) throws IOException {
//        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
//        List<PatientInfoDO> list = patientInfoService.getPatientInfoPageByCounty(pageReqVO).getList();
//        // 导出 Excel
//        ExcelUtils.write(response, "上报精神病患者信息.xls", "数据", PatientInfoRespVO.class,
//                BeanUtils.toBean(list, PatientInfoRespVO.class));
//    }

}
